<?php

	header("Content-Type: text/html;charset=utf-8");

	include("function/_nosql.php"); //
	
	include("function/function.php");

	include("_include.user.judge.php");

	$action = chkstr(trim($_REQUEST['action']));
	$kboy_userid = $_SESSION['kboy_userid'];

	$kboy_user = $_SESSION['kboy_user'];

	$order_realname = implode('@@',$_REQUEST['order_realname']);

	$order_phone = implode('@@',$_REQUEST['order_phone']);
	if(empty($_REQUEST['order_shangche'])){
		echo erro("请填写上车地");
	}
	//身份证正则表达式 
	$isIDCard2="/^(([0-9]{15})|([0-9]{18})|([0-9]{17}x))$/";
	foreach ($_REQUEST['order_idcard'] as $k => $v) {
		$zhenze = preg_match($isIDCard2, $v);
		if($zhenze == 0){
			echo erro("身份证错误！");
		}
		$a = substr($v,-2,1);
		if($a%2==0){
			$sex[] = 0;
		}else{
			$sex[] = 1;
		}
	}
	$order_sex = implode('@@',$sex);
	$order_idcard = implode('@@',$_REQUEST['order_idcard']);

	
	foreach ($_REQUEST['order_shangche'] as $k => $v) {
		if(empty($v)){
		echo erro("每个人的上车地址都要填写");
		}
	}
	
	$order_shangche = implode('@@',$_REQUEST['order_shangche']);
	
	if(is_array($_REQUEST['order_text'])){
		$order_text = implode('@@',$_REQUEST['order_text']);
	}else{
		$order_text =$_REQUEST['order_text'];
	}
	$order_payway = chkstr(trim($_REQUEST['rWay1']));

	$order_isfull = chkstr(trim($_REQUEST['order_isfull']));

	$order_num = count($_REQUEST['order_realname']);

	if($order_num == 0  ){

	  $order_num = 1;

	}

	$WIDout_trade_no = chkstr(trim($_REQUEST['WIDout_trade_no']));

	$aid = chkstr(trim($_REQUEST['aid']));

	if($action == "orderadd"){
		if(empty($aid)){

			echo erro("参数错误");

		}

		if(!is_numeric($aid)){

			echo erro("参数错误");

		}

		if(getNewsNums("kboy_forum_active","id=$aid") <1){

			echo erro("参数错误");

		}
		$mysql = new sqlstr("kboy_order");
		
		$mysql -> set("uid",$kboy_userid);
		
		$mysql -> set("aid",$aid);

		$mysql -> set("order_no",$WIDout_trade_no);

		$mysql -> set("order_realname",$order_realname);

		$mysql -> set("order_phone",$order_phone);

		$mysql -> set("order_idcard",$order_idcard);

		$mysql -> set("order_shangche",$order_shangche);

		$mysql -> set("order_text",$order_text);

		$mysql -> set("order_payway",$order_payway);

		$mysql -> set("order_isfull",$order_isfull);

		$mysql -> set("order_num",$order_num);
		
		$mysql -> set("order_sex",$order_sex);

		$news_sql = $mysql -> insertSql();

		$news_res = mysql_query($news_sql);
		
		if($news_res){

		   echo ok('提交成功','user_order.php');

		  }
	}

	if($action == "ordersave"){//保存订单信息

		if(empty($aid)){

			echo erro("参数错误");

		}

		if(!is_numeric($aid)){

			echo erro("参数错误");

		}

		if(getNewsNums("kboy_forum_active","id=$aid") <1){

			echo erro("参数错误");

		}
		$mysql = new sqlstr("kboy_order"); 

		$mysql -> set("order_realname",$order_realname);

		$mysql -> set("order_phone",$order_phone);

		$mysql -> set("order_idcard",$order_idcard);

		$mysql -> set("order_shangche",$order_shangche);

		$mysql -> set("order_text",$order_text);

		$mysql -> set("order_payway",$order_payway);

		$mysql -> set("order_isfull",$order_isfull);

		$mysql -> set("order_num",$order_num);
		
		$mysql -> set("order_sex",$order_sex);

		$news_sql = $mysql -> updateSql("aid='$aid' and uid='$kboy_userid' and order_no='$WIDout_trade_no'");

		$news_res = mysql_query($news_sql);

		if($news_res){

		  webAD($kboy_userid,"报名成功","恭喜您报名成功，您的订单号为".$WIDout_trade_no);

		  if(getClassName("kboy_forum_active","active_all_money","id=$aid") > 0 && $order_payway == 0 ){

		   echo re("alipay/alipayapi.php?WIDout_trade_no=".$WIDout_trade_no."&aid=".$aid);

		  }
		  else if( getClassName("kboy_forum_active","active_all_money","id=$aid") > 0 && $order_payway == 2 ){//微信支付
		    echo re("native.php?WIDout_trade_no=".$WIDout_trade_no."&aid=".$aid);
		  }

		  else{

		   echo ok('提交成功','user_order.php');

		  }

		}

		else{

		  echo erro("服务器内部错误");

		}

	}

	else{

		echo erro("参数错误");

	}

	

?>